<?php

namespace app\admin\model\company;

use think\Model;
use traits\model\SoftDelete;

class Enterprise extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'enterprise';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!isset($row['weigh']) || !$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function info()
    {
        return $this->belongsTo('Info', 'id', 'ent_id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 审核获取显示状态
     */
    public function getCurrentStatus($status = 0)
    {
        // 1. 审核中显示通过或者驳回
        // 2. 通过后显示是否禁用
        // 3. 驳回当前申请作废, 不显示按钮
        // 4. 禁用显示是否启用
        $arr = [
            ['1' => __('通过'), '2' => __('驳回')], 
            ['3' => __('禁用')],
            [], 
            ['1' => __('重新启用')]
        ];
        return $arr[$status] ?? '';
    }

}
